<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="/Public/css/css/css">
    </head>
    <body>
        <div id="loading"><img src="/Public/image/loading/gif"></div>
        <div id="mask"></div>
        <div id="login">
            <div class="typeText isChoose" onclick="changeLoginRegister($(this),1)">登录</div>
            <div class="typeText noChoose" onclick="changeLoginRegister($(this),2)">注册</div>
            <div class="login">
                <span class="textSpan">手机号：</span><input type="text" name="mobile"/><br><br>
                <span class="textSpan">密码：</span><input type="password" name="password"/><br><br>
                <button onclick="login()">立刻登录</button>
            </div>
            <div class="register">
                <span class="textSpan">手机号：</span><input type="text" name="mobile"/><br><br>
                <span class="textSpan">用户名：</span><input type="text" name="username"/><br><br>
                <span class="textSpan">密码：</span><input type="password" name="password"/><br><br>
                <button onclick="register()">立即注册</button>
            </div>
         </div>
        <div class="loginWindow">
            <div class="loginInfobackgroundImage">
                <img src="/Public/image/infoBack/jpg" style="width: 100%;">
            </div>
            <div class="loginInfo">
                <div class="loginPhoto"><img src="http://img.sciclass.cn/default/default_avatar.png"></div>
                <div class="loginName">科技学堂</div>
                <div class="loginMethod">
                    <div onclick="showNotice($(this));">好友请求<span style="display:none;">0</span></div>
                    <div onclick="logout(1);">退出登录</div>
                </div>
                <div class="searchUser">
                    <input type="text" value="" placeholder="查找好友" autocomplete="off">
                </div>
            </div>
            <div class="connectList">
                <div class="taps">
                    <div class="personTap isChoose" onclick="changeTaps(1)">好友</div>
                    <div class="groupTap" onclick="changeTaps(2)">群组</div>
                </div>
                <div class="personList"></div>
                <div class="groupList" style="display:none;"></div>
                <div class="groupAdd" onclick="methodGroup($(this),1);" style="display:none;">创建新群组</div>
            </div>
            <div class="searchList" style="display:none;">
                <div class="typeText">好友</div>
                <div class="typeText">群组</div>
                <div class="searchText" onclick="openSearchWindow($(this))">到查找面板找一下</div>
            </div>
        </div>
        <div class="eachPerson" id="cloneEachPerson">
            <div class="personPhoto" online="0"><img><span style="display:none;">0</span></div>
            <div class="personName" userId="0" onclick="talkOne($(this))"></div>
            <div class="personMethod" onclick="delPerson($(this))">删除</div>
        </div>
        <div class="eachGroup" id="cloneEachGroup">
            <div class="groupPhoto"><img><span style="display:none;">0</span></div>
            <div class="groupName" groupId="0" onclick="talkAll($(this))"></div>
            <div class="groupMethod" onclick=""></div>
            <input type="hidden" class="existUid" value="">
        </div>
        <div class="searchWindow">
            <div class="title">
                <span>好友及群组查找</span>
                <span class="closeBtn">关闭</span>
            </div>
            <div class="taps">
                <div class="personTap isChoose" onclick="changeTaps(3)">找人</div>
                <div class="groupTap" onclick="changeTaps(4)">找群</div>
            </div>
            <div class="searchUser">
                <input type="text" value="" placeholder="查找好友" autocomplete="off" onkeydown="enterIn(event,2);">
            </div>
            <div class="personList"></div>
            <div class="groupList" style="display:none;"></div>
        </div>
        <div class="eachPerson" id="cloneEachSearchPerson">
            <div class="personPhoto"><img></div>
            <div class="username"></div>
            <div class="mobile"></div>
            <div class="personMethod" userId="0" onclick="addPerson($(this))">+ 好友</div>
        </div>
        <div class="eachGroup" id="cloneEachSearchGroup">
            <div class="groupPhoto"><img></div>
            <div class="groupName"></div>
            <div class="groupMethod" groupId="0" onclick="addGroup($(this))">+ 群组</div>
        </div>
        <div class="connectWindow">
            <div class="title">
                <span></span>
                <span class="close" onclick="$(this).parent().parent().hide();$('#mask').css('z-index',2);">关闭</span>
            </div>
            <div class="userInfo">
                <div class="photo"><img></div>
                <div class="username"></div>
                <div class="mobile"></div>
                <input type="hidden" name="eventId" value="0">
                <input type="hidden" name="eventType" value="1">
            </div>
            <div class="applyMsg">
                <textarea onkeydown="enterIn(event,3);"></textarea>
                <div onclick="addConnect()">发送</div>
            </div>
        </div>
        <div class="groupWindow">
            <div class="title">
                <span>创建群组</span>
                <span class="closeBtn">关闭</span>
            </div>
            <div class="groupInfo">
                <div class="groupName">
                    <span class="textSpan">群组名称：</span><input type="text" name="groupName">
                </div>
                <div class="friendList"></div>
                <div class="chooseList"></div>
                <input type="hidden" class="chooseUid" value=",">
                <input type="hidden" class="existUid" value="">
                <input type="hidden" class="groupId" value="0">
                <div onclick="saveGroupInfo()" class="methodButton"></div>
                <div onclick="unsetGroup()" class="methodButton" style="display: none;">解散</div>
            </div>
        </div>
        <div class="noticeWindow">
            <div class="title">
                <span>好友请求</span>
                <span class="closeBtn">关闭</span>
            </div>
            <div class="noticeList"></div>
        </div>
        <div class="eachNotice" id="cloneEachNotice">
            <div class="photo"><img></div>
            <div class="username">
                <div></div><span></span>
            </div>
            <div class="method" methodId="0" methodType="0">
                <div class="beginTalk" style="display:none;">
                    <span onclick="talkWith($(this));">发起回话</span>
                </div>
                <div class="passMethod" style="display:none;">
                    <span onclick="connectMethod($(this),1)">通过</span>
                    <select onchange="changeChoose($(this))">
                        <option value="1">通过</option>
                        <option value="2">拒绝</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="chatWindow">
            <div class="chatUser" chatType="" chatId="" chatFlag="">
                <div></div>
                <span onclick="$('.chatWindow').hide();$(this).parent().attr('chatFlag','');">关闭</span>
            </div>
            <div class="chatDiv"></div>
            <div class="sendDiv">
                <textarea class="sendContent" onkeydown="enterIn(event,1);"></textarea>
                <div class="submitDiv">
                    <div onclick="sendMsg()">发送</div>
                </div>
            </div>
            <div class="memberList" style="display:none;">

            </div>
        </div>
        <div class="eachChat" id="cloneEachChat">
            <div class="userPhoto"><img></div>
            <div class="username"></div>
            <div class="chatTime"></div>
            <div class="chatContent"></div>
        </div>
        <div class="eachGroupPerson" id="cloneEachGroupPerson">
            <div class="personPhoto"><img></div>
            <div class="personName" userId="0"></div>
        </div>
        <div class="messageWindow" style="display:none;"></div>
    </body>
    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        var wsServer = 'ws://192.168.1.79:9502';
        var websocket = null;
        var lockReconnect = false;
        var loginId = 0;
        var userList = [];
        var personOver = 0;
        var groupOver = 0;
        $(document).ready(function () {
            getLoginStatus();
        });
        function getLoginStatus() {
            $.ajax({
                url:'/WebSocket/getSession',
                type:'post',
                dataType:'json',
                data:{id:1},
                success:function(data){
                    if(data.errorCode==0 && data.result.isLogin==1){
                        websocket = new WebSocket(wsServer+'?userId='+data.result.info.userId);
                        websocketInit(wsServer+'?userId='+data.result.info.userId);
                        setLoginInfo(data.result.info);
                    }else{
                        $("#mask").show();
                        $("#login").show();
                    }
                }
            });
        }
        function websocketInit(url) {
            websocket.onopen = function (evt) {
                console.log("connect open");
            };

            websocket.onclose = function (evt) {
                console.log("connect close");
                reconnect(url);
            };

            websocket.onmessage = function (evt) {
                console.log("have message："+evt.data);
                var data = JSON.parse(evt.data);
                if(data.type=="chat"){
                    var chatFlag = $(".chatWindow .chatUser").attr("chatFlag");
                    if(data.chatFlag==chatFlag){
                        addChatLine(data);
                        if(data.user.userId != loginId && data.chatFlag.indexOf('group')<0){
                            setMsgIsRead(data.user.userId);
                        }
                    }else{
                        if(data.chatFlag.indexOf('group')<0){
                            addPersonMsgNum(data.user);
                            showTip("您收到一条个人信息");
                        }else{
                            addGroupMsgNum(data.chat);
                            showTip("您收到一条群聊信息");
                        }
                    }
                }else if(data.type=="login"){
                    showTip("您的好友 "+data.user.username+" 上线了");
                    editPersonLine(data.user);
                }else if(data.type=="logout"){
                    showTip("您的好友 "+data.user.username+" 下线了");
                    editPersonLine(data.user);
                }else if(data.type=="notice"){
                    if(data.chatFlag=="0"){ // 申请加好友
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                    }else if(data.chatFlag=="1"){ // 被同意
                        addPersonLine(data.chat);
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="2"){ // 被拒绝
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="3"){ // 主动同意
                        addPersonLine(data.user);
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="4"){ // 主动拒绝
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="5"){ // 邀请加群
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                    }else if(data.chatFlag=="6"){ // 邀请方接收到通过邀请通知
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="7"){ // 邀请方接收到拒绝邀请通知
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="8"){ // 被邀请方通过邀请
                        addGroupLine(data.user);
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="9"){ // 被邀请方拒绝邀请
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="10"){ // 申请加群
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                    }else if(data.chatFlag=="11"){ // 被申请方通过申请
                        addGroupLine(data.chat);
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="12"){ // 被申请方拒绝申请
                        showTip(data.user.username+" "+data.content);
                        addNoticeNum();
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="13"){ // 申请人收到申请被通过通知
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="14"){ // 申请人收到申请被拒绝通知
                        changeShowBlock(data.chatFlag,data.user,data.chat);
                    }else if(data.chatFlag=="15"){ // 删除好友
                        removePersonLine(data.user);
                    }else if(data.chatFlag=="16"){ // 群友收到有人退出群组通知
                        if($(".chatWindow .chatUser").attr("chatFlag")=="group"+data.chat.groupId){
                            $(".chatWindow .memberList .eachGroupPerson").find(".personName[userId='"+data.user.userId+"']").parent().remove();
                        }
                    }else if(data.chatFlag=="17"){ // 群友收到群组解散通知
                        removeGroupLine(data.chat.groupId);
                    }
                }else if(data.type=="replace"){
                    logout(2);
                }
            };

            websocket.onerror = function (evt, e) {
                console.log('连接错误: ' + evt.data);
            };
        }
        function reconnect(url) {
            if(lockReconnect){
                return false;
            }
            lockReconnect = true;
            setTimeout(function () {
                websocket = new WebSocket(url);
                websocketInit(url);
                lockReconnect = false;
            },2000);
        }
        function changeLoginRegister(obj,flag){
            if(flag==1){
                obj.addClass('isChoose');
                obj.removeClass('noChoose');
                obj.next().addClass('noChoose');
                obj.next().addClass('isChoose');
                $("#login .login").show();
                $("#login .register").hide();
            }else{
                obj.addClass('isChoose');
                obj.removeClass('noChoose');
                obj.prev().addClass('noChoose');
                obj.prev().addClass('isChoose');
                $("#login .login").hide();
                $("#login .register").show();
            }
        }
        function register() {
            var mobile = $('.register input[name="mobile"]').val();
            var username = $('.register input[name="username"]').val();
            var password = $('.register input[name="password"]').val();
            if(!mobile){
                alert('请填写用户手机号');
                return false;
            }
            if(!username){
                alert('请填写用户名');
                return false;
            }
            if(!password){
                alert('请填写用户密码');
                return false;
            }
            $.ajax({
                url:'/WebSocket/register',
                type:'post',
                dataType:'json',
                data:{mobile:mobile,username:username,password:password},
                success:function(data){
                    alert(data.msg);
                    if(data.errorCode==0){
                        websocket = new WebSocket(wsServer+'?userId='+data.result.userId);
                        websocketInit();
                        setLoginInfo(data.result);
                        $("#mask").hide();
                        $("#login").hide();
                    }
                }
            });
        }
        function login() {
            var mobile = $('.login input[name="mobile"]').val();
            var password = $('.login input[name="password"]').val();
            if(!mobile){
                alert('请填写用户手机号');
                return false;
            }
            if(!password){
                alert('请填写用户密码');
                return false;
            }
            $.ajax({
                url:'/WebSocket/login',
                type:'post',
                dataType:'json',
                data:{mobile:mobile,password:password},
                success:function(data){
                    alert(data.msg);
                    if(data.errorCode==0){
                        websocket = new WebSocket(wsServer+'?userId='+data.result.userId);
                        websocketInit();
                        setLoginInfo(data.result);
                        $("#mask").hide();
                        $("#login").hide();
                    }
                }
            });
        }

        function setLoginInfo(info){
            $('#loading').show();
            loginId=info.userId;
            $('.loginWindow .loginInfo .loginPhoto img').attr("src", info.photo);
            $('.loginWindow .loginInfo .loginName').html(info.username);
            getPersonList();
            getGroupList();
            getNoticeNum();
            setTypeaheadSource();
        }
        function getPersonList(){
            $(".loginWindow .connectList .personList").html("");
            $.ajax({
                url:'/WebSocket/getPersonList',
                type:'post',
                dataType:'json',
                data:{id:1},
                success:function(data){
                    if(data.errorCode==0){
                        if(data.result.length>0){
                            $.each(data.result,function(i,n){
                                addPersonLine(n);
                            });
                        }else{
                            $(".loginWindow .connectList .personList").html('<div class="noPerson">你怎么还没有好友</div>');
                        }
                        personOver = 1;
                    }else if(data.errorCode==200){
                        $('#loading').hide();
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function addPersonLine(data) {
            $(".loginWindow .connectList .personList .noPerson").remove();
            var cloneObj = $("#cloneEachPerson").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".personPhoto").attr('online',data.onLine);
            if(data.onLine){
                cloneObj.find(".personPhoto img").attr("src", data.photo);
            }else{
                cloneObj.find(".personPhoto img").attr("src", "/Public/image/offline/png");
            }
            if(parseInt(data.msgNum)>=99){
                cloneObj.find(".personPhoto span").show();
                cloneObj.find(".personPhoto span").html(99);
            }else if(parseInt(data.msgNum)<=0 || isNaN(parseInt(data.msgNum))){
                cloneObj.find(".personPhoto span").hide();
                cloneObj.find(".personPhoto span").html(0);
            }else{
                cloneObj.find(".personPhoto span").show();
                cloneObj.find(".personPhoto span").html(parseInt(data.msgNum));
            }
            cloneObj.find(".personName").html(data.username);
            cloneObj.find(".personName").attr("userId",data.userId);
            $(".loginWindow .connectList .personList").append(cloneObj);
            eachArr={};
            eachArr["userId"]=data.userId;
            eachArr["photo"]=data.photo;
            eachArr["username"]=data.username;
            eachArr["type"]=1;
            userList.push(eachArr);
        }
        function editPersonLine(data) {
            var line=$(".loginWindow .connectList .personList .eachPerson").find("div.personName[userId='"+data.userId+"']").parents(".eachPerson");
            line.find(".personPhoto").attr('online',data.onLine);
            if(data.onLine){
                line.find(".personPhoto img").attr("src", data.photo);
            }else{
                line.find(".personPhoto img").attr("src", "/Public/image/offline/png");
            }
            line.find(".personPhoto img").css("border-radius","1rem");
        }
        function removePersonLine(data){
            $(".loginWindow .connectList .personList .eachPerson").find("div.personName[userId='"+data.userId+"']").parents(".eachPerson").remove();
            if($(".loginWindow .connectList .personList .eachPerson").length<=0){
                $(".loginWindow .connectList .personList").html('<div class="noPerson">你怎么还没有好友</div>');
            }
            $.each(userList,function(i,n){
                if(n.userId==data.userId && n.type==1){
                    userList.splice(i,1);
                    return false;
                }
            });
        }
        function getGroupList(){
            $(".loginWindow .connectList .groupList").html("");
            $.ajax({
                url:'/WebSocket/getGroupList',
                type:'post',
                dataType:'json',
                data:{id:1},
                success:function(data){
                    if(data.errorCode==0){
                        if(data.result.length>0){
                            $.each(data.result,function(i,n){
                                addGroupLine(n);
                            });
                        }else{
                            $(".loginWindow .connectList .groupList").html('<div class="noGroup">你怎么还没有群组</div>');
                        }
                        groupOver = 1;
                    }else if(data.errorCode==200){
                        $('#loading').hide();
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function addGroupLine(data) {
            $(".loginWindow .connectList .groupList .noGroup").remove();
            var cloneObj = $("#cloneEachGroup").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".groupPhoto img").attr("src", "/Public/image/groupIcon/png");
            if(parseInt(data.msgNum)>=99){
                cloneObj.find(".groupPhoto span").show();
                cloneObj.find(".groupPhoto span").html(99);
            }else if(parseInt(data.msgNum)<=0 || isNaN(parseInt(data.msgNum))){
                cloneObj.find(".groupPhoto span").hide();
                cloneObj.find(".groupPhoto span").html(0);
            }else{
                cloneObj.find(".groupPhoto span").show();
                cloneObj.find(".groupPhoto span").html(parseInt(data.msgNum));
            }
            cloneObj.find(".groupName").html(data.name);
            cloneObj.find(".groupName").attr("groupId",data.id);
            if(data.owner_id == loginId){
                cloneObj.find(".groupMethod").html("编辑");
                cloneObj.find(".groupMethod").attr("onclick","methodGroup($(this),2);");
                cloneObj.find(".existUid").val(data.groupUser);
            }else{
                cloneObj.find(".groupMethod").html("退出");
                cloneObj.find(".groupMethod").attr("onclick","leaveGroup($(this));");
            }
            $(".loginWindow .connectList .groupList").append(cloneObj);
            eachArr={};
            eachArr["userId"]=data.id;
            eachArr["photo"]="/Public/image/groupIcon/png";
            eachArr["username"]=data.name;
            eachArr["type"]=2;
            userList.push(eachArr);
        }
        function editGroupLine(data) {
            var line=$(".loginWindow .connectList .groupList .eachGroup").find("div.groupName[groupId='"+data.id+"']").parents(".eachGroup");
            line.find(".groupName").html(data.name);
        }
        function removeGroupLine(groupId){
            if($(".chatWindow .chatUser").attr("chatFlag")=="group"+groupId){
                $(".chatWindow .chatDiv").html('');
                $(".chatWindow .memberList").html('');
                $(".chatWindow").hide();
            }
            $(".loginWindow .connectList .groupList .eachGroup").find("div.groupName[groupId='"+groupId+"']").parents(".eachGroup").remove();
            if($(".loginWindow .connectList .groupList .eachGroup").length<=0){
                $(".loginWindow .connectList .groupList").html('<div class="noGroup">你怎么还没有群组</div>');
            }
            $.each(userList,function(i,n){
                if(n.userId==groupId && n.type==2){
                    userList.splice(i,1);
                    return false;
                }
            });
        }
        function getNoticeNum(){
            $.ajax({
                url:'/WebSocket/getNoticeNum',
                type:'post',
                dataType:'json',
                data:{id:1},
                success:function(data){
                    if(data.errorCode==0){
                        if(parseInt(data.result)>=99){
                            $(".loginWindow .loginInfo .loginMethod div span").show();
                            $(".loginWindow .loginInfo .loginMethod div span").html(99);
                        }else if(parseInt(data.result)<=0){
                            $(".loginWindow .loginInfo .loginMethod div span").hide();
                            $(".loginWindow .loginInfo .loginMethod div span").html(0);
                        }else{
                            $(".loginWindow .loginInfo .loginMethod div span").show();
                            $(".loginWindow .loginInfo .loginMethod div span").html(parseInt(data.result));
                        }
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function setTypeaheadSource(){
            if(!personOver || !groupOver){
                setTimeout(function(){setTypeaheadSource()},1000)
                return false;
            }
            $('#loading').hide();
        }
        function changeTaps(tap){
            switch(tap){
                case 1:
                    $('.loginWindow .connectList .taps .personTap').addClass('isChoose');
                    $('.loginWindow .connectList .taps .groupTap').removeClass('isChoose');
                    $('.loginWindow .connectList .personList').show();
                    $('.loginWindow .connectList .groupList').hide();
                    $('.loginWindow .connectList .groupAdd').hide();
                    break;
                case 2:
                    $('.loginWindow .connectList .taps .personTap').removeClass('isChoose');
                    $('.loginWindow .connectList .taps .groupTap').addClass('isChoose');
                    $('.loginWindow .connectList .personList').hide();
                    $('.loginWindow .connectList .groupList').show();
                    $('.loginWindow .connectList .groupAdd').show();
                    break;
                case 3:
                    $('.searchWindow .taps .personTap').addClass('isChoose');
                    $('.searchWindow .taps .groupTap').removeClass('isChoose');
                    $('.searchWindow .personList').show();
                    $('.searchWindow .groupList').hide();

                    $(".searchWindow .pageDiv").remove();
                    var key = $(".searchWindow .searchUser input").val();
                    doUserSearch(key);
                    break;
                case 4:
                    $('.searchWindow .taps .personTap').removeClass('isChoose');
                    $('.searchWindow .taps .groupTap').addClass('isChoose');
                    $('.searchWindow .personList').hide();
                    $('.searchWindow .groupList').show();

                    $(".searchWindow .pageDiv").remove();
                    var key = $(".searchWindow .searchUser input").val();
                    doUserSearch(key);
                    break;
                default:
                    return false;
            }
        }
        function showTip(msg){
            if($('.messageWindow').is(":visible")){
                $('.messageWindow').hide();
            }
            $('.messageWindow').html(msg);
            $('.messageWindow').fadeIn("normal",function(){
                setTimeout("$('.messageWindow').fadeOut('normal')",2000);
            });
        }
        function talkOne(obj){
            setSearchResult('')
            obj.prev().find("span").html('0');
            obj.prev().find("span").hide();
            var chatId=obj.attr('userId');
            doTalkOne(chatId,obj.html());
        }
        function doTalkOne(chatId,chatName){
            var chatFlag = loginId>chatId?chatId+""+loginId:loginId+""+chatId;
            if($(".chatWindow .chatUser").attr("chatFlag") == chatFlag){
                return false;
            }
            $(".chatWindow .chatUser div").html("正在和 "+chatName+" 聊天中...");
            $(".chatWindow .chatUser").attr("chatFlag",chatFlag);
            $(".chatWindow .chatUser").attr("chatType","1");
            $(".chatWindow .chatUser").attr("chatId",chatId);
            $(".chatWindow .chatDiv").html('');
            $(".chatWindow").show();
            $.ajax({
                url:'/WebSocket/getRecentMsg',
                type:'post',
                dataType:'json',
                data:{userId:chatId},
                success:function(data){
                    if(data.errorCode==0){

                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
            $(".chatWindow .memberList").hide();
            $(".chatWindow .chatDiv").css("right","0.5rem");
        }
        function talkAll(obj){
            setSearchResult('')
            obj.prev().find("span").html('0');
            obj.prev().find("span").hide();
            var chatId=obj.attr('groupId');
            doTalkAll(chatId,obj.html());
        }
        function doTalkAll(chatId,chatName){
            var chatFlag = "group"+chatId;
            if($(".chatWindow .chatUser").attr("chatFlag") == chatFlag){
                return false;
            }
            $(".chatWindow .chatUser div").html("正在和 "+chatName+" 群里的小伙伴群聊中...");
            $(".chatWindow .chatUser").attr("chatFlag",chatFlag);
            $(".chatWindow .chatUser").attr("chatType","2");
            $(".chatWindow .chatUser").attr("chatId",chatId);
            $(".chatWindow .chatDiv").html('');
            $(".chatWindow .memberList").html('');
            $(".chatWindow").show();
            $.ajax({
                url:'/WebSocket/getGroupRecentMsg',
                type:'post',
                dataType:'json',
                data:{groupId:chatId},
                success:function(data){
                    if(data.errorCode==0){

                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
            $.ajax({
                url:'/WebSocket/getGroupPerson',
                type:'post',
                dataType:'json',
                data:{groupId:chatId},
                success:function(data){
                    if(data.errorCode==0){
                        $.each(data.result,function(i,n){
                            var cloneObj = $("#cloneEachGroupPerson").clone();
                            cloneObj.removeAttr('id');
                            if(n.onLine){
                                cloneObj.find(".personPhoto img").attr("src", n.photo);
                            }else{
                                cloneObj.find(".personPhoto img").attr("src", "/Public/image/offline/png");
                            }
                            cloneObj.find(".personName").attr("userId",n.userId);
                            cloneObj.find(".personName").html(n.username);
                            $(".chatWindow .memberList").append(cloneObj);
                        });
                        $(".chatWindow .memberList").show();
                        $(".chatWindow .chatDiv").css("right","10.7rem");
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function setMsgIsRead(chatId){
            $.ajax({
                url:'/WebSocket/setMsgIsRead',
                type:'post',
                dataType:'json',
                data:{userId:chatId},
                success:function(data){
                    if(data.errorCode==0){

                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function sendMsg() {
            var content = $('.chatWindow .sendDiv .sendContent').val();
            if(!content){
                alert('内容为空，不可发言。');
                return false;
            }
            if(!loginId){
                $("#mask").show();
                $("#login").show();
                return false;
            }
            var chatType = $(".chatWindow .chatUser").attr("chatType");
            var chatId = $(".chatWindow .chatUser").attr("chatId");
            websocket.send(
                JSON.stringify({
                    sendId:loginId,
                    chatType:chatType,
                    chatId:chatId,
                    content:content
                })
            );
            $('.sendContent').val('');
        }
        function enterIn(evt,flag){
            var event=evt?evt:(window.event?window.event:null);//兼容IE和FF
            if (event.keyCode===13){
                event.preventDefault();
                if(flag==1){
                    sendMsg();
                }else if(flag==2){
                    $(".searchWindow .pageDiv").remove();
                    var key = $(".searchWindow .searchUser input").val();
                    doUserSearch(key);
                }else if(flag==3){
                    addConnect();
                }
            }
        }
        function addPersonMsgNum(data){
            var span=$(".loginWindow .connectList .personList .eachPerson").find("div.personName[userId='"+data.userId+"']").prev().find("span");
            var num = parseInt(span.html())>=99?parseInt(span.html()):parseInt(span.html())+1;
            if(num == 1){
                span.show();
            }
            span.html(num);
        }
        function addGroupMsgNum(data){
            var span=$(".loginWindow .connectList .groupList .eachGroup").find("div.groupName[groupId='"+data.groupId+"']").prev().find("span");
            var num = parseInt(span.html())>=99?parseInt(span.html()):parseInt(span.html())+1;
            if(num == 1){
                span.show();
            }
            span.html(num);
        }
        function addNoticeNum(){
            var span=$(".loginWindow .loginInfo .loginMethod div span");
            var num = parseInt(span.html())>=99?parseInt(span.html()):parseInt(span.html())+1;
            if(num == 1){
                span.show();
            }
            span.html(num);
        }
        function addChatLine(data) {
            var cloneObj = $("#cloneEachChat").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".userPhoto img").attr("src",data.user.photo);
            cloneObj.find(".username").html(data.user.username);
            cloneObj.find(".chatTime").html(data.time);
            cloneObj.find(".chatContent").html(data.content);
            $(".chatWindow .chatDiv").append(cloneObj);
            $(".chatWindow .chatDiv")[0].scrollTop=$(".chatWindow .chatDiv")[0].scrollHeight;
        }
        var inputLock = false;
        $('.loginWindow .loginInfo .searchUser input')[0].addEventListener('compositionstart', function(){
            inputLock = true;
        });
        $('.loginWindow .loginInfo .searchUser input')[0].addEventListener('compositionend', function(){
            inputLock = false;
            if(!inputLock){
                setSearchResult(this.value);
            }
        });
        $('.loginWindow .loginInfo .searchUser input')[0].addEventListener('input', function(){
            if(!inputLock){
                setSearchResult(this.value);
            }
        });
        function setSearchResult(key){
            $(".loginWindow .searchList .eachPerson").remove();
            $(".loginWindow .searchList .eachGroup").remove();
            if(key.length<=0){
                $('.loginWindow .loginInfo .searchUser input').val('');
                $(".loginWindow .connectList").show();
                $(".loginWindow .searchList").hide();
                return false;
            }else{
                $(".loginWindow .connectList").hide();
                $(".loginWindow .searchList").show();
            }
            $.each(userList,function(i,n){
                if(n.username.indexOf(key)>=0){
                    if(n.type==1){
                        var cloneObj = $("#cloneEachPerson").clone();
                        cloneObj.removeAttr('id');
                        cloneObj.find(".personPhoto img").attr("src", n.photo);
                        cloneObj.find(".personName").html(n.username);
                        cloneObj.find(".personName").attr("userId",n.userId);
                        cloneObj.find(".personMethod").html('');
                        $(".loginWindow .searchList .typeText").eq(0).after(cloneObj);
                    }else{
                        var cloneObj = $("#cloneEachGroup").clone();
                        cloneObj.removeAttr('id');
                        cloneObj.find(".groupPhoto img").attr("src", "/Public/image/groupIcon/png");
                        cloneObj.find(".groupName").html(n.groupName);
                        cloneObj.find(".groupName").attr("groupId",n.groupId);
                        cloneObj.find(".groupMethod").html('');
                        $(".loginWindow .searchList .typeText").eq(1).after(cloneObj);
                    }
                }
            });
            if($(".loginWindow .searchList .eachPerson").length<=0){
                $(".loginWindow .searchList .typeText").eq(0).hide();
            }else{
                $(".loginWindow .searchList .typeText").eq(0).show();
            }
            if($(".loginWindow .searchList .eachGroup").length<=0){
                $(".loginWindow .searchList .typeText").eq(1).hide();
            }else{
                $(".loginWindow .searchList .typeText").eq(1).show();
            }
        }
        function openSearchWindow(obj){
            key = $('.loginWindow .loginInfo .searchUser input').val();
            $(".searchWindow .searchUser input").val(key);
            setSearchResult('');
            doUserSearch(key);
        }
        var searchPage=1;
        var totalPage=0;
        var maxShowPage=4;
        function doUserSearch(key){
            if($('.searchWindow .taps .personTap').hasClass('isChoose')){
                var url='/WebSocket/searchUser';
                var showType = 1;
                $(".searchWindow .personList").html('');
            }else if($('.searchWindow .taps .groupTap').hasClass('isChoose')){
                var url='/WebSocket/searchGroup';
                var showType = 2;
                $(".searchWindow .groupList").html('');
            }else{
                return false;
            }
            $.ajax({
                url:url,
                type:'post',
                dataType:'json',
                data:{key:key,page:searchPage},
                success:function(data){
                    if(data.errorCode==0){
                        $("#mask").show();
                        $(".searchWindow").show();
                        if(data.errorCode==0){
                            if(data.result && data.result.totalCount>0){
                                if(showType==1){
                                    $.each(data.result.users,function(i,n){
                                        addSearchPersonLine(n);
                                    });
                                }else{
                                    $.each(data.result.groups,function(i,n){
                                        addSearchGroupLine(n);
                                    });
                                }
                                totalPage = data.result.totalPage;
                                var pageHtml = getPage(totalPage,searchPage);
                                $(".searchWindow .pageDiv").remove();
                                if(pageHtml==''){
                                    $('.searchWindow .personList').css('height',$('.searchWindow').height()-5.5*16);
                                    $(".searchWindow .groupList").css('height',$('.searchWindow').height()-5.5*16);
                                }else{
                                    $('.searchWindow .personList').css('height',$('.searchWindow').height()-5.5*16-3*16);
                                    $(".searchWindow .groupList").css('height',$('.searchWindow').height()-5.5*16-3*16);
                                    $(".searchWindow").append(pageHtml);
                                }
                            }else{
                                if(showType==1){
                                    $(".searchWindow .personList").html('<div class="noResult">未搜索到结果，请重新搜索。</div>');
                                }else{
                                    $(".searchWindow .groupList").html('<div class="noResult">未搜索到结果，请重新搜索。</div>');
                                }
                            }
                        }
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        $(document).delegate(".searchWindow .pageDiv ul li","click", function(){
            searchPage =  parseInt($(this).data("pageat"));
            var key = $(".searchWindow .searchUser input").val();
            if(searchPage > 0 && searchPage <= totalPage){
                doUserSearch(key);
            }
        });
        function getPage(totalPages,currentPage) {
            var prefix = '<div class="pageDiv"><ul>';
            var suffix = '</ul></div>';
            var items = '';
            if (totalPages == null || totalPages == undefined || totalPages <= 1) {
                return items;
            }
            else if (totalPages > 1 && totalPages <= maxShowPage) {
                for (var i = 0; i < totalPages; i++) {
                    if (i == (currentPage - 1)) {
                        items += '<li class="active pages" data-pageat=' + (i + 1) + ' ><span>' + (i + 1) + '</span></li>';
                    }
                    else {
                        items += '<li class="pages" data-pageat=' + (i + 1) + '  ><span>' + (i + 1) + '</span></li>';
                    }
                }
                return prefix + items + suffix;
            }
            else if (totalPages > maxShowPage) {
                var prevItems='';
                var nextItems='';
                var middle_page=maxShowPage%2==1?Math.ceil(maxShowPage/2):maxShowPage/2;
                var pages_list=[];
                if (currentPage<=middle_page) {
                    for (var i = 1,j = 1; j <= maxShowPage; i++,j++) {
                        pages_list.push(i-currentPage);
                    }
                    nextItems += '<li class="pages pages_next" data-pageat="'+(currentPage+1)+'"  ><span>'+'>'+'</span></li>'
                    nextItems += '<li class="pages pages_foot" data-pageat="'+totalPages+'" ><span>'+totalPages+'</span></li>';
                }else if(totalPages-currentPage<maxShowPage){
                    prevItems += '<li class="pages pages_head" data-pageat="1"><span>'+'1'+'</span></li>';
                    prevItems += '<li class="pages pages_prev" data-pageat="'+(currentPage-1)+'" ><span>'+'<'+'</span></li>';
                    for (var i = 0,j = 1; j <= maxShowPage; i++,j++) {
                        pages_list.push(totalPages-currentPage-i);
                    }
                    pages_list.reverse();
                }else{
                    prevItems += '<li class="pages pages_head" data-pageat="1"><span>'+'1'+'</span></li>';
                    prevItems += '<li class="pages pages_prev" data-pageat="'+(currentPage-1)+'" ><span>'+'<'+'</span></li>';
                    for (var i = 1; i <= maxShowPage; i++) {
                        pages_list.push(i-middle_page);
                    }
                    nextItems += '<li class="pages pages_next" data-pageat="'+(currentPage+1)+'"  ><span>'+'>'+'</span></li>'
                    nextItems += '<li class="pages pages_foot" data-pageat="'+totalPages+'" ><span>'+totalPages+'</span></li>';
                }
                $.each(pages_list,function(i,n){
                    if (n == 0) {
                        items += '<li class="active pages" data-pageat=' + (currentPage+n) + '  ><span>' + (currentPage+n) + '</span></li>';
                    }
                    else {
                        items += '<li class="pages" data-pageat=' + (currentPage+n) + '><span  >' + (currentPage+n) + '</span></li>';
                    }
                });
                return prefix + prevItems + items + nextItems +suffix;
            }
        }
        function addSearchPersonLine(data){
            var cloneObj = $("#cloneEachSearchPerson").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".personPhoto img").attr("src", data.photo);
            cloneObj.find(".username").html(data.username);
            cloneObj.find(".mobile").html(data.mobile);
            cloneObj.find(".personMethod").attr("userId",data.userId);
            $(".searchWindow .personList").append(cloneObj);
        }
        function addSearchGroupLine(data){
            var cloneObj = $("#cloneEachSearchGroup").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".groupPhoto img").attr("src", '/Public/image/groupIcon/png');
            cloneObj.find(".groupName").html(data.groupName);
            cloneObj.find(".groupMethod").attr("groupId",data.groupId);
            $(".searchWindow .groupList").append(cloneObj);
        }
        $('.closeBtn').click(function(){
            searchPage=1;
            noticePage=1;
            totalPage=0;
            totalNoticePage=0;
            $(this).parent().parent().hide();
            $('#mask').hide();
        });
        function addPerson(obj){
            $("#mask").css("z-index",3);
            var personInfo=obj.parent();
            $(".connectWindow .title span").eq(0).html("添加好友");
            $(".connectWindow .userInfo .photo img").attr("src",personInfo.find('.personPhoto img').attr("src"));
            $(".connectWindow .userInfo .username").html(personInfo.find('.username').html());
            $(".connectWindow .userInfo .mobile").html(personInfo.find('.mobile').html());
            $(".connectWindow .userInfo input[name='eventId']").val(personInfo.find('.personMethod').attr('userId'));
            $(".connectWindow .userInfo input[name='eventType']").val(1);
            $('.connectWindow .applyMsg textarea').focus();
            $(".connectWindow").show();
        }
        function addGroup(obj){
            $("#mask").css("z-index",3);
            var groupInfo=obj.parent();
            $(".connectWindow .title span").eq(0).html("申请加群");
            $(".connectWindow .userInfo .photo img").attr("src",groupInfo.find('.groupPhoto img').attr("src"));
            $(".connectWindow .userInfo .username").html(groupInfo.find('.groupName').html());
            $(".connectWindow .userInfo .mobile").html();
            $(".connectWindow .userInfo input[name='eventId']").val(groupInfo.find('.groupMethod').attr('groupId'));
            $(".connectWindow .userInfo input[name='eventType']").val(2);
            $('.connectWindow .applyMsg textarea').focus();
            $(".connectWindow").show();
        }
        function addConnect(){
            $(".connectWindow").hide();
            $('#mask').css('z-index',2);
            var eventType=$(".connectWindow .userInfo input[name='eventType']").val();
            var eventId=$(".connectWindow .userInfo input[name='eventId']").val();
            var content=$(".connectWindow .applyMsg textarea").val();
            if(!loginId){
                $("#mask").show();
                $("#login").show();
                return false;
            }
            $.ajax({
                url:'/WebSocket/addConnect',
                type:'post',
                dataType:'json',
                data:{eventType:eventType,eventId:eventId,content:content},
                success:function(data){
                    if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        var noticePage=1;
        var totalNoticePage=0;
        var maxShowPage=4;
        function showNotice(obj){
            $('#mask').show();
            $('.noticeWindow').show();
            $('.noticeWindow .noticeList').html('');
            obj.find("span").html('0');
            obj.find("span").hide();
            doNoticeShow();
        }
        function doNoticeShow(){
            $.ajax({
                url:'/WebSocket/getNoticeList',
                type:'post',
                dataType:'json',
                data:{page:noticePage},
                success:function(data){
                    if(data.errorCode==0){
                        if(data.result.totalCount>0){
                            $.each(data.result.users,function(i,n){
                                addNoticeLine(n);
                            });
                            totalNoticePage = data.result.totalPage;
                            var pageHtml = getPage(totalNoticePage,noticePage);
                            $(".noticeWindow .pageDiv").remove();
                            if(pageHtml==''){
                                $('.noticeWindow .noticeList').css('height',$('.searchWindow').height()-3*16);
                            }else{
                                $('.noticeWindow .noticeList').css('height',$('.searchWindow').height()-3*16-3*16);
                                $(".noticeWindow").append(pageHtml);
                            }
                        }else{
                            $(".noticeWindow .noticeList").html('<div class="noResult">暂无相关信息</div>');
                        }
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        $(document).delegate(".noticeWindow .pageDiv ul li","click", function(){
            noticePage =  parseInt($(this).data("pageat"));
            if(noticePage > 0 && noticePage <= totalNoticePage){
                doNoticeShow();
            }
        });
        function addNoticeLine(data){
            var cloneObj = $("#cloneEachNotice").clone();
            cloneObj.removeAttr('id');
            cloneObj.find(".method").attr('sendId',data.sendId);
            cloneObj.find(".method").attr('methodId',data.methodId);
            cloneObj.find(".method").attr('methodType',data.noticeType);
            if(data.noticeType==1){
                if(data.sendId == loginId){
                    cloneObj.find(".photo img").attr("src", data.methodInfo.photo);
                    cloneObj.find(".username div").html(data.methodInfo.username);
                    if(data.status==0){
                        cloneObj.find(".username span").html('尚未处理您的好友请求');
                    }else if(data.status==1){
                        cloneObj.find(".username span").html('通过了您的好友请求');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-2){
                        cloneObj.find(".username span").html('拒绝了您的好友请求');
                    }
                }else{
                    cloneObj.find(".photo img").attr("src", data.sendInfo.photo);
                    cloneObj.find(".username div").html(data.sendInfo.username);
                    if(data.status==0){
                        cloneObj.find(".username span").html('请求加您为好友');
                        cloneObj.find(".method .passMethod").show();
                    }else if(data.status==1){
                        cloneObj.find(".username span").html('请求加您为好友，已同意');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-1){
                        cloneObj.find(".username span").html('请求加您为好友，已拒绝');
                    }
                }
            }else if(data.noticeType==2){
                if(data.sendId == loginId){
                    cloneObj.find(".photo img").attr("src", '/Public/image/groupIcon/png');
                    cloneObj.find(".username div").html(data.methodInfo.name);
                    if(data.status==0){
                        cloneObj.find(".username span").html('尚未处理您的加群请求');
                    }else if(data.status==1){
                        cloneObj.find(".username span").html('通过了您的加群请求');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-1){
                        cloneObj.find(".username span").html('拒绝了您的加群请求');
                    }
                }else{
                    cloneObj.find(".photo img").attr("src", data.sendInfo.photo);
                    cloneObj.find(".username div").html(data.sendInfo.username);
                    if(data.status==0){
                        cloneObj.find(".username span").html(data.sendInfo.username+'申请加入'+data.methodInfo.name);
                        cloneObj.find(".method .passMethod").show();
                    }else if(data.status==1){
                        cloneObj.find(".username span").html(data.sendInfo.username+'申请加入'+data.methodInfo.name+',已同意');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-1){
                        cloneObj.find(".username span").html(data.sendInfo.username+'申请加入'+data.methodInfo.name+',已拒绝');
                    }
                }
            }else if(data.noticeType==3){
                if(data.sendInfo.owner_id == loginId){
                    cloneObj.find(".photo img").attr("src", data.methodInfo.photo);
                    cloneObj.find(".username div").html(data.methodInfo.username);
                    if(data.status==0){
                        cloneObj.find(".username span").html('尚未处理您的加群邀请');
                    }else if(data.status==1){
                        cloneObj.find(".username span").html('通过了您的加群邀请');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-1){
                        cloneObj.find(".username span").html('拒绝了您的加群邀请');
                    }
                }else{
                    cloneObj.find(".photo img").attr("src", '/Public/image/groupIcon/png');
                    cloneObj.find(".username div").html(data.sendInfo.name);
                    if(data.status==0){
                        cloneObj.find(".username span").html('邀请您加入群聊');
                        cloneObj.find(".method .passMethod").show();
                    }else if(data.status==1){
                        cloneObj.find(".username span").html('邀请您加入群聊,已同意');
                        cloneObj.find(".method .beginTalk").show();
                    }else if(data.status==-1){
                        cloneObj.find(".username span").html('邀请您加入群聊,已拒绝');
                    }
                }
            }
            $(".noticeWindow .noticeList").append(cloneObj);
        }
        function talkWith(obj){
            noticePage=1;
            totalNoticePage=0;
            $('.noticeWindow .noticeList').html('');
            $('#mask').hide();
            $('.noticeWindow').hide();
            var chatType = obj.parent().parent().attr('methodType');
            if(chatType == 1){
                var sendId = obj.parent().parent().attr('sendId');
                var methodId = obj.parent().parent().attr('methodId');
                if(sendId == loginId){
                    doTalkOne(methodId,obj.parent().parent().prev().find('div').html());
                }else{
                    doTalkOne(sendId,obj.parent().parent().prev().find('div').html());
                }
            }else if(chatType == 2){
                doTalkAll(obj.parent().parent().attr('methodId'),obj.parent().parent().prev().find('div').html());
            }else if(chatType == 3){
                doTalkAll(obj.parent().parent().attr('sendId'),obj.parent().parent().prev().find('div').html());
            }
        }
        function connectMethod(obj,flag){
            var sendId = obj.parent().parent().attr('sendId');
            var methodId = obj.parent().parent().attr('methodId');
            var methodType = obj.parent().parent().attr('methodType');
            var method = flag;
            $.ajax({
                url:'/WebSocket/connectMethod',
                type:'post',
                dataType:'json',
                data:{sendId:sendId,methodId:methodId,methodType:methodType,method:method},
                success:function(data){
                    if(data.errorCode==0){

                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function changeChoose(obj){
            if(obj.val()==1){
                connectMethod(obj.prev(),1);
            }else{
                connectMethod(obj.prev(),0);
            }
        }
        function changeShowBlock(flag,send,chat){
            if($('.noticeWindow').is(":visible")){
                if(flag<5){
                    var eachNoticeMethod = $(".noticeWindow .noticeList .eachNotice").find("div.method[sendId='"+send.userId+"'][methodId='"+chat.userId+"']");
                    eachNoticeMethod.find('.passMethod').hide();
                    switch(flag){
                        case 1:
                            eachNoticeMethod.prev().find('span').html('通过了您的好友请求');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 2:
                            eachNoticeMethod.prev().find('span').html('拒绝了您的好友请求');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                        case 3:
                            eachNoticeMethod.prev().find('span').html('请求加您为好友，已同意');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 4:
                            eachNoticeMethod.prev().find('span').html('请求加您为好友，已拒绝');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                    }
                }else if(flag<10){
                    var eachNoticeMethod = $(".noticeWindow .noticeList .eachNotice").find("div.method[sendId='"+send.id+"'][methodId='"+chat.userId+"']");
                    eachNoticeMethod.find('.passMethod').hide();
                    switch(flag){
                        case 6:
                            eachNoticeMethod.prev().find('span').html('通过了您的加群邀请');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 7:
                            eachNoticeMethod.prev().find('span').html('拒绝了您的加群邀请');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                        case 8:
                            eachNoticeMethod.prev().find('span').html('邀请您加入群聊，已同意');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 9:
                            eachNoticeMethod.prev().find('span').html('邀请您加入群聊，已拒绝');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                    }
                }else{
                    var eachNoticeMethod = $(".noticeWindow .noticeList .eachNotice").find("div.method[sendId='"+send.userId+"'][methodId='"+chat.id+"']");
                    eachNoticeMethod.find('.passMethod').hide();
                    switch(flag){
                        case 11:
                            eachNoticeMethod.prev().find('span').html('通过了您的加群申请');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 12:
                            eachNoticeMethod.prev().find('span').html('拒绝了您的加群申请');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                        case 13:
                            eachNoticeMethod.prev().find('span').html('申请加入群聊，已同意');
                            eachNoticeMethod.find('.beginTalk').show();
                            break;
                        case 14:
                            eachNoticeMethod.prev().find('span').html('申请加入群聊，已拒绝');
                            eachNoticeMethod.find('.beginTalk').hide();
                            break;
                    }
                }
            }
        }
        function delPerson(obj){
            var methodId=obj.prev().attr('userId');
            $.ajax({
                url:'/WebSocket/delPerson',
                type:'post',
                dataType:'json',
                data:{methodId:methodId},
                success:function(data){
                    if(data.errorCode==0){
                        showTip('已与 '+obj.prev().html()+' 解除好友关系');
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function logout(flag){
            $.ajax({
                url:'/WebSocket/logout',
                type:'post',
                dataType:'json',
                data:{id:1},
                success:function(){
                    loginId = 0;
                    userList = [];
                    personOver = 0;
                    groupOver = 0;
                    lockReconnect = true;
                    if(flag==1){
                        alert('已成功退出');
                    }else{
                        alert('已在其他地方登陆');
                    }
                    websocket.close();
                    window.location.reload();
                }
            });
        }
        function methodGroup(obj,flag){
            $("#mask").show();
            $(".groupWindow").show();
            $(".groupWindow .groupInfo input.chooseUid").val(',');
            if(flag==1){
                $(".groupWindow .title span").eq(0).html('创建群组');
                $(".groupWindow .groupInfo .methodButton").eq(0).html('创建');
                $(".groupWindow .groupInfo .methodButton").eq(1).hide();
            }else if(flag==2){
                $(".groupWindow .title span").eq(0).html('编辑群组');
                $('.groupWindow .groupInfo .groupName input').val(obj.prev().html());
                $('.groupWindow .groupInfo .existUid').val(obj.next().val());
                $('.groupWindow .groupInfo .groupId').val(obj.prev().attr('groupId'));
                $(".groupWindow .groupInfo .methodButton").eq(0).html('保存');
                $(".groupWindow .groupInfo .methodButton").eq(1).show();
            }else{
                return false;
            }
            $(".groupWindow .groupInfo .friendList").html('');
            $(".groupWindow .groupInfo .chooseList").html('');
            $.each(userList,function(i,n){
                if(n.type==1){
                    var cloneObj = $("#cloneEachPerson").clone();
                    cloneObj.removeAttr('id');
                    cloneObj.attr("onclick","addGroupChoose($(this))");
                    cloneObj.find(".personPhoto img").attr("src", n.photo);
                    cloneObj.find(".personName").html(n.username);
                    cloneObj.find(".personName").attr("userId",n.userId);
                    cloneObj.find(".personName").attr("onclick","return false;");
                    cloneObj.find(".personMethod").html('');
                    $(".groupWindow .groupInfo .friendList").append(cloneObj);
                }
            });
        }
        function addGroupChoose(obj){
            var userIds = obj.parents('.groupInfo').find("input.chooseUid").val();
            var existIds = $('.groupInfo').find("input.existUid").val();
            var userId = obj.find(".personName").attr("userId");
            if(userIds.indexOf(','+userId+',')>=0){
                alert('已选择该好友');
                return false;
            }
            if(existIds.indexOf(','+userId+',')>=0){
                alert('该好友已在群组中');
                return false;
            }
            obj.parents('.groupInfo').find("input.chooseUid").val(userIds+userId+',');
            var cloneObj = obj.clone();
            cloneObj.attr("onclick","removeGroupChoose($(this))");
            $(".groupWindow .groupInfo .chooseList").append(cloneObj);
        }
        function removeGroupChoose(obj){
            var userIds = obj.parents('.groupInfo').find("input.chooseUid").val();
            var userId = obj.find(".personName").attr("userId");
            if(userIds.indexOf(','+userId+',')>=0){
                obj.parents('.groupInfo').find("input.chooseUid").val(userIds.replace(','+userId+',',','));
            }
            obj.remove();
        }
        function saveGroupInfo(){
            var groupId = $('.groupWindow .groupInfo .groupId').val();
            var groupName = $('.groupWindow .groupInfo .groupName input').val();
            if(!groupName){
                alert('群组名不能为空。');
                return false;
            }
            if(!loginId){
                $("#mask").show();
                $("#login").show();
                return false;
            }
            var chooseUid = $(".groupWindow .groupInfo input.chooseUid").val();
            $.ajax({
                url:'/WebSocket/saveGroupInfo',
                type:'post',
                dataType:'json',
                data:{groupId:groupId,groupName:groupName,chooseUid:chooseUid},
                success:function(data){
                    if(data.errorCode==0){
                        changeTaps(2);
                        $('.groupWindow').hide();
                        $('#mask').hide();
                        if(groupId == 0){
                            addGroupLine(data.result);
                        }else{
                            editGroupLine(data.result);
                        }
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function leaveGroup(obj){
            var methodId=obj.prev().attr('groupId');
            $.ajax({
                url:'/WebSocket/leaveGroup',
                type:'post',
                dataType:'json',
                data:{methodId:methodId},
                success:function(data){
                    if(data.errorCode==0){
                        alert('已退出 '+obj.prev().html()+' 群组');
                        removeGroupLine(methodId);
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
        function unsetGroup(obj){
            var groupId = $('.groupWindow .groupInfo .groupId').val();
            if(!loginId){
                $("#mask").show();
                $("#login").show();
                return false;
            }
            $.ajax({
                url:'/WebSocket/unsetGroup',
                type:'post',
                dataType:'json',
                data:{groupId:groupId},
                success:function(data){
                    if(data.errorCode==0){
                        changeTaps();
                        $('.groupWindow').hide();
                        $('#mask').hide();
                        if(groupId == 0){
                            addGroupLine(data.result);
                        }else{
                            editGroupLine(data.result);
                        }
                    }else if(data.errorCode==200){
                        $("#mask").show();
                        $("#login").show();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }
    </script>
</html>
